<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="主键" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tid">
              <a-input-number v-model="model.tid" placeholder="请输入主键" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="任务主键" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskTid">
              <a-input-number v-model="model.taskTid" placeholder="请输入任务主键" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="任务查询的sql" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskSql">
              <a-textarea v-model="model.taskSql" rows="4" placeholder="请输入任务查询的sql" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="sql关联的数据源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceId">
              <a-input-number v-model="model.sourceId" placeholder="请输入sql关联的数据源" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="同步方式全量还是增量 （0全量  1增量）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="syncType">
              <a-input v-model="model.syncType" placeholder="请输入同步方式全量还是增量 （0全量  1增量）"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="增量字段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="incrementField">
              <a-input v-model="model.incrementField" placeholder="请输入增量字段"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="增量扩展时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="extraDur">
              <a-input-number v-model="model.extraDur" placeholder="请输入增量扩展时间" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="添加方式(insert,replace)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appendType">
              <a-input v-model="model.appendType" placeholder="请输入添加方式(insert,replace)"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CollectTaskSqlForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           tid: [
              { required: true, message: '请输入主键!'},
           ],
           taskTid: [
              { required: true, message: '请输入任务主键!'},
           ],
        },
        url: {
          add: "/dcloud-platform/api/collectTaskSql/v1/add",
          edit: "/dcloud-platform/api/collectTaskSql/v1/edit",
          queryById: "/dcloud-platform/api/collectTaskSql/v1/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>